  // pages/user/cooperation/index.js
  import citys from '../../resource/data/pc.js';
  import { $wuxCalendar } from '../../miniprogram_npm/wux-weapp/index'
  import {
    getTrainingCooperation
  } from '../../api/other.js';
  import{
    getSpecialityOption,
    getResumeInfo,
    submitResume
  }from '../../api/course.js';
  const app = getApp()
  Page({

  /**
   * 页面的初始数据
   */
  data: {
    isApply: false,
    cityOptions: citys,
    ExpectcityOptions: citys,
    NowcityOptions: citys,
    isShowCitys: false,
    isShowExpectCitys: false,
    isShowNowCitys: false,
    isShowSpeciality:false,
    showSex: false,
    showSalary:false,
    WorkTimeShow:false,
    salaryOptions:['4k-6k','6k-8k','8k-10k','10k-15k'],//薪资option
    specialityOptions:[],//专业类别option
    sexType: 1, // 1-男 2-女
    name: '', //姓名
    isOpen:true,//是否公开
    wechatPhone: '',//微信
    birthday: '', //出生日期
    area: '',//居住地
    jobCategory: '', //求职类别
    salary: '', //期望薪资
    citys: '', 
    Expectarea: '', //期望城市
    Nowarea: '', //现住地
    colleges: '', //毕业院校
    speciality: '', //专业类别
    mail: '', //邮箱
    WorkTime: '', //从业时间
    resumeId:'',//简历id
    authCode: '',
    authBtnText: '获取验证码',
    countdown: 60,
    isSend: false,
    companyId:0,
    showCustom:false,
    jobCategoryArr:[],
    inputJobCategory:"",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log("options",options)
    app.listener.promise().then(()=>{
      this.loadSpecialityOption()
      this.data.companyId=options.companyId
      getTrainingCooperation(res => {
        this.setData({
          isApply: res.isApply,
          wechatPhone: res.mobile,
        })
      })
      this.loadResumeInfo()
      console.log(this.data)
    })
    if(app.globalData.memberId){
      app.listener.resolve()
    }
  },

  /**
   * 加载简历详情
   */
  loadResumeInfo(){
    getResumeInfo().then(res => {
      console.log(res)
      this.setData({
        isOpen:res.isOpen,//是否公开
        sexType: res.sex, // 1-男 2-女
        name: res.realName, //姓名
        birthday:this.tsFormatTime(res.birthday), //出生日期
        area: res.hometown,//籍贯
        jobCategory: res.classifyIds, //求职类别
        salary: res.salayRange, //期望薪资
        Expectarea: res.provice, //期望城市
        Nowarea: res.address, //现住地
        colleges: res.school, //毕业院校
        speciality: res.classifyMyid, //专业类别
        mail: res.email, //邮箱
        WorkTime:this.tsFormatTime(res.graduationTime), //从业时间
        id:res.id,//简历id
        wechatPhone:res.wechat,//微信
      })
    })
  },

  /**
   * 提交申请
   */
  handleCommitApply() {
    if (this.data.name === '') {
      this.showToast('请输入姓名');
    } else if (this.data.area === '') {
      this.showToast('请选择居住地');
    } else if (this.data.Nowarea === '') {
      this.showToast('请选择现居地');
    } else if (this.data.sexType === '') {
      this.showToast('请选择性别');
    } else if (this.data.birthday === '') {
      this.showToast('请选择出生日期');
    } else if (this.data.speciality === '') {
      this.showToast('请选择专业类别');
    } else if (!/^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/.test(this.data.mail)) {
      this.showToast('请输入正确格式的邮箱');
    } else if (this.data.workTime === '') {
      this.showToast('请选择毕业时间');
    } else if (!/^1[3|4|5|6|8|7|9][0-9]\d{8}$/.test(this.data.wechatPhone)) {
      this.showToast('请输入正确的微信号');
    } 
    else {
      const companyId = this.data.companyId?this.data.companyId:0
      console.log(companyId)
      console.log(this.data.birthday)
      // wx.request({
      //   url: 'http://192.168.0.68:8081/training-api/resume_info/create_edit?companyId='+companyId,
      //   method:"put",
      //   data:{
      //     realName: this.data.name, //姓名
      //     provice: this.data.Expectarea, //期望城市
      //     address: this.data.Nowarea, //现住地
      //     hometown: this.data.area, //籍贯
      //     sex: this.data.sexType, //性别
      //     birthday:this.data.birthday,
      //     classifyIds: this.data.jobCategory, //求职类别
      //     salayRange: this.data.salary, //期望薪资
      //     classifyMyid: this.data.speciality, //专业类别
      //     graduationTime: this.data.WorkTime, //从业时间
      //     email: this.data.mail, //邮箱
      //     wechat: this.data.wechatPhone, //微信
      //     isOpen:this.data.isOpen,//是否公开
      //     id:this.data.id,//简历id
      //     memberId:getApp().globalData.memberId,//必填
      //   },
      //   success:(res)=>{
      //     this.showToast('提交成功');
      //     console.log(this.data)
      //     console.log('http://192.168.0.68:8888/',res.data)
      //   },
      // })
      submitResume(companyId,{
        realName: this.data.name, //姓名
        provice: this.data.Expectarea, //期望城市
        address: this.data.Nowarea, //现住地
        hometown: this.data.area, //籍贯
        sex: this.data.sexType, //性别
        birthday:this.data.birthday,
        classifyIds: this.data.jobCategory, //求职类别
        salayRange: this.data.salary, //期望薪资
        school: this.data.colleges, //毕业院校
        classifyMyid: this.data.speciality, //专业类别
        graduationTime: this.data.WorkTime, //从业时间
        email: this.data.mail, //邮箱
        wechat: this.data.wechatPhone, //微信
        isOpen:this.data.isOpen,//是否公开
        id:this.data.id,//简历id
        memberId:getApp().globalData.memberId,//必填
      }).then(() => {
        
        this.showToast('提交成功');
        wx.switchTab({
          url: '../user/mine',
        })
      })
    }
  },
    /**
   * 格式化时间
   */
  tsFormatTime(timestamp) {
    if(timestamp==undefined){
      return ''
    }
    let date = new Date(timestamp);
    let year = date.getFullYear()
    let month = (date.getMonth() + 1)<10?('0'+(date.getMonth() + 1)):(date.getMonth() + 1)
    let day = date.getDate()<10?('0'+date.getDate()):date.getDate()
    return year+'-'+month+'-'+day;
  } ,
  openCustom(){
    this.setData({
      showCustom:true
    })
  },
  closeCustom(){
  this.data.jobCategory = this.data.jobCategoryArr.join()
    this.setData({
      showCustom:false,
      jobCategory:this.data.jobCategoryArr.join()
    })
    console.log(this.data.jobCategory)
  },
   /**
   * 求职类别弹出
   */
  onChangeCustom(e) {
    console.log(e)
    let Arr = this.data.jobCategoryArr
    let current = e.currentTarget.dataset.title
    if(Arr.indexOf(current)<0){
      Arr.push(e.currentTarget.dataset.title)
      this.setData({
        jobCategoryArr: Arr
      })
    }
  },
  removeCategory(e) {
    let arr = this.data.jobCategoryArr
    let index = arr.indexOf(e.currentTarget.dataset.title)
    if (index > -1) {
      arr.splice(index, 1);
    }
    this.setData({
      jobCategoryArr: arr
    })
  },

  /**
   * 倒计时
   */
  countDown() {
    if (this.data.countdown === 0) {
      this.setData({
        isSend: false,
        authBtnText: '重新获取',
        countdown: 60,
      })
      return
    } else {
      this.setData({
        isSend: true,
        authBtnText: `${this.data.countdown--}s`
      })
    }
    let timer = setTimeout(() => {
      clearTimeout(timer);
      this.countDown()
    }, 1000)
  },
  onChange(field, e) {
      this.setData({
        [field]: e.detail.value
      })
      console.log('radio发生change事件，携带value值为：', e.detail.value)
    },
    changeIsOpen(e) {
      console.log(e)
      this.onChange('isOpen', e);
      this.setData({
        isOpen: e.detail.value
      })
    },
 
    /**
   * 选择地区
   * @param {object} e 
   */
  handleChangeCitys(e) {
    let label = e.detail.options.map((n) => n.label).join(' ');
    let value = e.detail.options.map((n) => n.value);
    if (e.detail.options.length === 2) {
      this.setData({
        area: label,
        isShowCitys: false
      })
    }
  },

    /**
   * 选择现住地址
   * @param {object} e 
   */
  handleChangeNowCitys(e) {
    let label = e.detail.options.map((n) => n.label).join(' ');
    let value = e.detail.options.map((n) => n.value);
    if (e.detail.options.length === 2) {
      this.setData({
        Nowarea: label,
        isShowNowCitys: false
      })
    }
  },

      /**
   * 期望城市
   * @param {object} e 
   */
    handleChangeExpectCitys(e) {
      let label = e.detail.options.map((n) => n.label).join(' ');
      let value = e.detail.options.map((n) => n.value);
      if (e.detail.options.length === 2) {
        this.setData({
          Expectarea: label,
          isShowExpectCitys: false
        })
      }
    },
    handleOpenSpeciality() {
      this.setData({
        isShowSpeciality: true
      })
    },
    handleOpenCascasder() {
      this.setData({
        isShowCitys: true
      })
    },
    handleOpenExpectCascasder() {
      this.setData({
        isShowExpectCitys: true
      })
    },
    handleOpenNowCascasder() {
      this.setData({
        isShowNowCitys: true
      })
    },

  /**
   * 姓名
   * @param {object} e 
   */
  onInputName(e) {
    this.data.name = e.detail.value.trim();
  },

  /**
   * 性别
   * @param {*} e 
   */
  handleChangeSexType(e) {
    this.setData({
      sexType: Number(e.currentTarget.dataset.type)
    })
  },

  /**
   * 期望薪资
   * @param {object} e 
   */
  onChangeSalary(e) {
    this.setData({
      salary: this.data.salaryOptions[Number(e.detail.value)]
    });
  },
  /**
   * 出生日期
   */
  // onInputBirthday(e){
  //   $wuxCalendar().open({
  //     value: this.data.birthday,
  //     onChange: (values, displayValues) => {
  //       console.log('onChange', values, displayValues)
  //       this.setData({
  //         birthday: displayValues[0],
  //       })
  //     },
  //   })
  // },
    /**
   * 出生年月
   */
  onChangeTime(e) {
    this.setData({
      birthday: e.detail.value
    });
  },

    /**
   * 从业时间
   */
  // onInputWorkTime(e){
  //   $wuxCalendar().open({
  //     WorkTime: this.data.WorkTime,
  //     onChange: (values, displayValues) => {
  //       console.log('onChange', values, displayValues)
  //       this.setData({
  //         WorkTime: displayValues[0],
  //       })
  //     },
  //   })
  // },
  onChangeWorkTime(e) {
    this.setData({
      WorkTime: e.detail.value
    });
  },
  
  /**
   * 求职类别
   * @param {object} e 
   */
  onChangeJobCategory(e){
    this.setData({
      jobCategory: this.data.specialityOptions[Number(e.detail.value)]
    });
  },
  
  /**
   * 毕业院校
   * @param {object} e 
   */
  onInputColleges(e) {
    this.data.colleges = e.detail.value.trim();
  },
  
  /**
   * 专业类别
   * @param {object} e 
   */
  onChangeSpeciality(e) {
    console.log(this.data.specialityOptions[Number(e.detail.value)])
    this.setData({
      speciality: this.data.specialityOptions[Number(e.detail.value)]
    });
  },

    /**
     * 获取专业类别
     * @param {object} e 
     */
  loadSpecialityOption() {
    getSpecialityOption().then(res => {
      let array = []
      for (let i = 0; i < res.length; i++) {
        array.push(res[i].name)
      }
      this.setData({
        specialityOptions: array
      })
    })
  },
  
  /**
   * 邮箱
   * @param {object} e 
   */
  onInputMail(e) {
    this.data.mail = e.detail.value.trim();
  },

  /**
   * 微信
   * @param {object} e 
   */
  onInputMobile(e) {
    this.data.wechatPhone = e.detail.value.trim();
  },

  /**
   * 验证码
   * @param {object} e 
   */
  onInputAuthCode(e) {
    this.data.authCode = e.detail.value.trim();
  },

  /**
   * 吐司
   * @param {string} msg 
   */
  showToast(msg) {
    wx.showToast({
      title: msg,
      icon: 'none'
    })
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})